package com.mashibing.juc.c_019;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用ReentrantLock还可以调用lockInterruptibly方法，可以对线程interrupt方法做出响应
 * 在一个线程等待过程中可以被打断
 * ReentrantLock还可以指定为公平锁
 * ReentrantLock默认是非公平锁
 * @author: Yuanzt
 * @Date: 2021/3/26
 */
public class T05_ReentrantLock5 extends Thread{

    private static ReentrantLock lock = new ReentrantLock(true);//参数true表示是公平锁

    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " 获得锁");
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T05_ReentrantLock5 tr = new T05_ReentrantLock5();
        Thread t1 = new Thread(tr);
        Thread t2 = new Thread(tr);
        t1.start();
        t2.start();
    }

}
